package cn.dvptech.remotecontrol.screenmirror.audio;

import android.annotation.TargetApi;
import android.media.AudioAttributes;
import android.media.AudioFormat;
import android.media.AudioPlaybackCaptureConfiguration;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.media.projection.MediaProjection;
import android.os.Build;

import cn.dvptech.remotecontrol.screenmirror.configuration.AudioConfiguration;

/**
 *    author  : ulongx
 *    website : www.dvptech.cn
 *    date    : 2024/7/30
 *    desc    :
 */
public class AudioUtils {
    public static boolean checkMicSupport(AudioConfiguration audioConfiguration, MediaProjection mediaProjection) {
        boolean result;
        int recordBufferSize = getRecordBufferSize(audioConfiguration);
        byte[] mRecordBuffer = new byte[recordBufferSize];
        AudioRecord audioRecord = getAudioRecord(audioConfiguration, mediaProjection);
        try {
            audioRecord.startRecording();
        } catch (Exception e) {
            e.printStackTrace();
        }
        int readLen = audioRecord.read(mRecordBuffer, 0, recordBufferSize);
        result = readLen >= 0;
        try {
            audioRecord.release();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static int getRecordBufferSize(AudioConfiguration audioConfiguration) {
        int frequency = audioConfiguration.frequency;
        int audioEncoding = audioConfiguration.encoding;
        int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
        if(audioConfiguration.channelCount == 2) {
            channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_STEREO;
        }
        return AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding);
    }

    /**
     * 录音源的，其可选参数如下：
     * MediaRecorder.AudioSource.CAMCORDER 设定录音来源于同方向的相机麦克风相同，若相机无内置相机或无法识别，则使用预设的麦克风
     * MediaRecorder.AudioSource.DEFAULT  默认音频源
     * MediaRecorder.AudioSource.MIC 设定录音来源为主麦克风。
     * MediaRecorder.AudioSource.VOICE_CALL 设定录音来源为语音拨出的语音与对方说话的声音
     * MediaRecorder.AudioSource.VOICE_COMMUNICATION 摄像头旁边的麦克风
     * MediaRecorder.AudioSource.VOICE_DOWNLINK 下行声音
     * MediaRecorder.AudioSource.VOICE_RECOGNITION 语音识别
     * MediaRecorder.AudioSource.VOICE_UPLINK 上行声音
     * @param audioConfiguration 配置信息
     * @return
     */
    @TargetApi(18)
    public static AudioRecord getAudioRecord(AudioConfiguration audioConfiguration, MediaProjection mediaProjection) {
        int frequency = audioConfiguration.frequency;
        int audioEncoding = audioConfiguration.encoding;
        int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
        if(audioConfiguration.channelCount == 2) {
            channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_STEREO;
        }

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q){
            AudioPlaybackCaptureConfiguration config = new AudioPlaybackCaptureConfiguration.Builder(mediaProjection)
                    .addMatchingUsage(AudioAttributes.USAGE_MEDIA) // 可以根据需要添加更多匹配条件
                    .build();

            return new AudioRecord.Builder()
                    .setAudioPlaybackCaptureConfig(config) // 使用 AudioPlaybackCaptureConfig，而不是 AudioSource
                    .setAudioFormat(new AudioFormat.Builder()
                            .setEncoding(audioEncoding)
                            .setSampleRate(frequency)
                            .setChannelMask(channelConfiguration)
                            .build())
                    .setBufferSizeInBytes(getRecordBufferSize(audioConfiguration)) // 设定适合的缓冲区大小
                    .build();
        } else {
            int audioSource = MediaRecorder.AudioSource.DEFAULT;//默认语音
//        if (audioConfiguration.mic){
//            audioSource =  MediaRecorder.AudioSource.MIC;//麦克风
//        }
            if(audioConfiguration.aec) {
                audioSource = MediaRecorder.AudioSource.VOICE_COMMUNICATION;
            }
            return new AudioRecord(audioSource, frequency,
                    channelConfiguration, audioEncoding, getRecordBufferSize(audioConfiguration));
        }

    }
}
